package com.dragon.mmochat.model.gang;

import java.util.Comparator;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.MmochatDailyActivity;
import com.dragon.mmochat.model.enumType.MmochatDailyActivityType;
import com.dragon.mmochat.model.enumType.MmochatGangLevelType;
import com.dragon.mmochat.model.enumType.MmochatGangMemberType;
import com.dragon.mmochat.model.enumType.MmochatGangSkillType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

//帮派
public class MmochatGang implements Comparable<MmochatGang> {
	private int id;
	private String name;
	private String info; // 公告
	private int mapId;
	private int roomA_mapId; // 房间A地图ID
	private int roomB_mapId; // 房间B地图ID
	private MmochatGangLevelType level = MmochatGangLevelType.初级;
	private int buildValue = 0; // 建设度
	private Date lastDayClearMembersWeekBuildValue = null; // 每周一0点清除成员每周贡献值，此变量保存上一次清除的时间
	private int money = 0; // 帮派资金
	private Map<Integer, MmochatGangMember> members = new ConcurrentHashMap<Integer, MmochatGangMember>();
	private Map<MmochatGangSkillType, MmochatGangSkill> skills = new ConcurrentHashMap<MmochatGangSkillType, MmochatGangSkill>();

	// 内存数据
	private long lastTickOfSendingMsgToAll = 0; // 最近一次群发消息的时间戳
	// 申请入帮人员
	private Map<Integer, MmochatGangRequester> requesters = new ConcurrentHashMap<Integer, MmochatGangRequester>();
	// 帮派比赛积分
	private int gangActivityScore = 0;
	private boolean delete = false;

	private int fuBenId = 0; // 0则无副本
	// 日常活动
	private Map<MmochatDailyActivityType, MmochatDailyActivity> dailyActivities = new ConcurrentHashMap<MmochatDailyActivityType, MmochatDailyActivity>();

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getMapId() {
		return mapId;
	}

	public void setMapId(int mapId) {
		this.mapId = mapId;
	}

	public MmochatGangLevelType getLevel() {
		return level;
	}

	public void setLevel(MmochatGangLevelType level) {
		this.level = level;
	}

	public int getBuildValue() {
		return buildValue;
	}

	public void setBuildValue(int buildValue) {
		this.buildValue = buildValue;
	}

	public void addBuildValue(int value) {
		buildValue += value;
		buildValue = Math.max(0, buildValue);
	}

	public int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public void addMoney(int addMoney) {
		if ((long) Integer.MAX_VALUE - money < addMoney) {
			money = Integer.MAX_VALUE;
		} else {
			money += addMoney;
			money = Math.max(0, money);
		}
	}

	public Map<Integer, MmochatGangMember> getMembers() {
		return members;
	}

	public void setMembers(Map<Integer, MmochatGangMember> members) {
		this.members = new ConcurrentHashMap<Integer, MmochatGangMember>(
				members);
	}

	public Map<MmochatGangSkillType, MmochatGangSkill> getSkills() {
		return skills;
	}

	public void setSkills(Map<MmochatGangSkillType, MmochatGangSkill> skills) {
		this.skills = new ConcurrentHashMap<MmochatGangSkillType, MmochatGangSkill>(
				skills);
	}

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public boolean canSendMsgToAll() {
		return System.currentTimeMillis() - lastTickOfSendingMsgToAll >= MmochatConstant.intervalOfGangSendingMsgToAll;
	}

	public void updateSendMsgToAllTick() {
		lastTickOfSendingMsgToAll = System.currentTimeMillis();
	}

	public int getMemberNumByType(MmochatGangMemberType memberType) {
		int count = 0;
		for (MmochatGangMember member : members.values()) {
			if (member.getType() == memberType) {
				count++;
			}
		}
		return count;
	}

	// 相应职位的玩家是否已满
	public boolean isMemberTypeFull(MmochatGangMemberType memberType) {
		int count = 0;
		switch (memberType) {
		case 帮主:
			return true;
		case 副帮:
			count = getMemberNumByType(memberType);
			if (count >= level.getMaxViceMasterNum()) {
				return true;
			}
			return false;
		case 精英:
			count = getMemberNumByType(memberType);
			if (count >= level.getMaxVipMemberNum()) {
				return true;
			}
			return false;
		case 帮众:
			return false;
		default:
			break;
		}
		return true;
	}

	public Map<Integer, MmochatGangRequester> getRequesters() {
		return requesters;
	}

	public void setRequesters(Map<Integer, MmochatGangRequester> requesters) {
		this.requesters = requesters;
	}

	// 申请人数是否已满
	public boolean isRequestFull() {
		return requesters.size() >= level.getMaxMemberNum();
	}

	// 成员是否已满
	public boolean isMemberFull() {
		return members.size() >= level.getMaxMemberNum();
	}

	public int getRoomA_mapId() {
		return roomA_mapId;
	}

	public void setRoomA_mapId(int roomAMapId) {
		roomA_mapId = roomAMapId;
	}

	public int getRoomB_mapId() {
		return roomB_mapId;
	}

	public void setRoomB_mapId(int roomBMapId) {
		roomB_mapId = roomBMapId;
	}

	public int getGangActivityScore() {
		return gangActivityScore;
	}

	public void setGangActivityScore(int gangActivityScore) {
		this.gangActivityScore = gangActivityScore;
	}

	public void addGangActivityScore(int add) {
		gangActivityScore += add;
	}

	public Date getLastDayClearMembersWeekBuildValue() {
		return lastDayClearMembersWeekBuildValue;
	}

	public void setLastDayClearMembersWeekBuildValue(
			Date lastDayClearMembersWeekBuildValue) {
		this.lastDayClearMembersWeekBuildValue = lastDayClearMembersWeekBuildValue;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	// TODO:Gson分割线
	public String getGson_Members() {
		if (members == null) {
			return null;
		}
		Gson gson = new Gson();
		return gson.toJson(members,
				new TypeToken<Map<Integer, MmochatGangMember>>() {
				}.getType());
	}

	public String getGson_Skills() {
		if (skills == null) {
			return null;
		}
		Gson gson = new Gson();
		return gson.toJson(skills,
				new TypeToken<Map<MmochatGangSkillType, MmochatGangSkill>>() {
				}.getType());
	}

	@Override
	public int compareTo(MmochatGang gang) {
		// 先按等级，再按建设度
		if (level.getValue() != gang.getLevel().getValue()) {
			return level.getValue() - gang.getLevel().getValue();
		}
		return buildValue - gang.getBuildValue();
	}

	// 帮派比赛积分排序,降序
	public static class mixedGangBattleScoreSort implements
			Comparator<MmochatGang> {
		@Override
		public int compare(MmochatGang arg0, MmochatGang arg1) {
			return arg1.getGangActivityScore() - arg0.getGangActivityScore();
		}
	}

	public long getLastTickOfSendingMsgToAll() {
		return lastTickOfSendingMsgToAll;
	}

	public void setLastTickOfSendingMsgToAll(long lastTickOfSendingMsgToAll) {
		this.lastTickOfSendingMsgToAll = lastTickOfSendingMsgToAll;
	}

	public int getFuBenId() {
		return fuBenId;
	}

	public void setFuBenId(int fuBenId) {
		this.fuBenId = fuBenId;
	}

	public Map<MmochatDailyActivityType, MmochatDailyActivity> getDailyActivities() {
		return dailyActivities;
	}

	public void setDailyActivities(
			Map<MmochatDailyActivityType, MmochatDailyActivity> dailyActivities) {
		this.dailyActivities = new ConcurrentHashMap<MmochatDailyActivityType, MmochatDailyActivity>(
				dailyActivities);
	}

	public String getGson_dailyActivities() {
		if (dailyActivities == null) {
			return null;
		}
		Gson gson = new Gson();
		return gson
				.toJson(
						dailyActivities,
						new TypeToken<Map<MmochatDailyActivityType, MmochatDailyActivity>>() {
						}.getType());
	}
	
	public boolean isInFuBenActivity() {
		return fuBenId != 0;
	}

	public void closeFuBen() {
		fuBenId = 0;
	}
	
	public void addDailyActivity(MmochatDailyActivity activity) {
		if (activity != null) {
			dailyActivities.put(activity.getType(), activity);
		}
	}
}
